import QtQuick 2.0
import QtQuick.Controls 2.14
import QtQuick.Layouts 1.14
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.4
//import QtQuick.Controls.impl 2.4

 Popup{
    id:root
    property string type: "add"      // 弹出框的类型 add | delete
    property string bgColor: "#fff"  // 弹出框背景颜色
    property int radius: 8
    property string textColor : "black"
    property string title: "xxx"
    property var colorMap : {"add":"#1989FA","delete":"red"}
    property string btnName: "add"
    property string curretPrintFile: ""

    property var listModel: []

    closePolicy:Popup.NoAutoClose
    modal: true
    visible: false
    background: Rectangle{
        id:innerRec
        border.color: root.colorMap[root.type]
        border.width: 2
        implicitHeight: 330
        implicitWidth: 550
        radius: root.radius

        ListView{
           id:listView
           anchors.fill:innerRec
           anchors.leftMargin: 10
           anchors.rightMargin: 10
           anchors.topMargin: 5
           anchors.bottomMargin: 5
           spacing: 5
           clip: true
           ExclusiveGroup { id: exclusive }
           model: root.listModel//printControl.fileList
           delegate:Rectangle{
               width:parent.width
               height: 55
               color: "#c6c6c6"

               RowLayout{
                   anchors.fill: parent
                   anchors.leftMargin: 5
                   spacing: 5
                   RadioButton{
                       id: control
                       Layout.alignment: Qt.AlignCenter | Qt.AlignHCenter
                       exclusiveGroup: exclusive
                       style:RadioButtonStyle{
                           indicator: Rectangle{
                               implicitHeight: 20;
                               implicitWidth: 20;
                               radius: 10;
                               border.color: root.colorMap[root.type]
                               border.width: 1;

                               Rectangle{
                                   anchors.fill: parent;
                                   visible: control.checked;
                                   color:root.colorMap[root.type];
                                   radius: 10;
                                   anchors.margins: 3;
                               }
                           }
                       }

                       onClicked: {
                           if(root.type == "add"){
                                root.curretPrintFile = modelData["abs_path"];
                           }else{
                                root.curretPrintFile = modelData["uuid"];
                           }
                       }
                    }

                   Rectangle{
                       width: 50
                       height: 50
                       Image {
                           id:fileIcon
                           width: 50
                           height: 50
                           source: "qrc:/imgs/untype.png"
                       }
                   }

                   Connections{
                       target: showImage
                       function onCallQmlRefeshImg(){
                           if("dir" === modelData["type"]) return;
                           fileIcon.source = "image://imageprovider/" + modelData["name"]
                       }
                   }

                  /* Rectangle{
                       width: 300
                       color: "red" //"transparent"
                       height: parent.height
                       Layout.alignment: Qt.AlignCenter | Qt.AlignHCenter
                       Label{
                          id:pathLable
                          font.pixelSize: 18
                          text: modelData["name"] //+ modelData["abs_path"]

                          anchors.centerIn: parent
                       }
                   }*/
                   Label{
                      id:pathLable
                      font.pixelSize: 18
                      text:  root.type == "add" ? modelData["abs_path"] : modelData["uuid"]
                      Layout.alignment: Qt.AlignLeft

                   }
                   Rectangle{
                    Layout.fillWidth: true
                   }
                }
            }

           ScrollBar.vertical: ScrollBar {
               parent: listView.parent    //设置另一个父级
               anchors.top: listView.top
               anchors.right: listView.right
               anchors.rightMargin:-10
               anchors.bottom: listView.bottom
            }
          }
        }

        Rectangle{
            width:parent.width
            height: 55
            color: "transparent"
            anchors.bottom: parent.bottom
            anchors.bottomMargin: -70

            RowLayout{
                anchors.fill: parent
                spacing: 50
                Rectangle{
                   Layout.fillWidth: true
                }
                Rectangle{
                    id:btn1
                    width: 90
                    height: 55
                    radius: 4
                    color: root.colorMap[root.type]

                    Text {
                        font.pixelSize: 22
                        font.bold: true
                        text: root.btnName
                        color: "#fff"
                        anchors.centerIn: parent
                    }

                    MouseArea{
                        anchors.fill: parent
                        onPressed: {
                            parent.scale *= .9
                        }
                        onReleased:{
                            parent.scale /= .9
                            if(root.type == "add"){
                                printControl.selectPrintFile(root.curretPrintFile)
                            }else{
                                printControl.removePrintWork(root.curretPrintFile)
                            }

                            root.close()
                        }
                    }
                }
                Rectangle{
                    id:btn2
                    width: 90
                    height: 55
                    radius: 4
                    color: root.colorMap[root.type]

                    Text {
                        font.pixelSize: 22
                        font.bold: true
                        text: qsTr("取 消")
                        color: "#fff"
                        anchors.centerIn: parent
                    }
                    MouseArea{
                        anchors.fill: parent
                        onPressed: {
                            parent.scale *= .9
                        }
                        onReleased:{
                            parent.scale /= .9
                            root.close()
                        }
                    }
                }
                Rectangle{
                   Layout.fillWidth: true
                }
            }
        }


    Text {
        id: textTitle
        text: root.title
        anchors.top : parent.top
        anchors.left: parent.left
        font.pixelSize: 26
        font.bold: true
        color: root.colorMap[root.type]
        anchors.topMargin: -47
        anchors.leftMargin: -10
    }

    Rectangle {
        id: closeBtn
        width: 40
        height: 40
        color: root.colorMap[root.type]
        radius: width / 2
        anchors.top : parent.top
        anchors.right: parent.right
        anchors.topMargin: -30
        anchors.rightMargin: -30
        Text {
            id: name
            text: "X"
            anchors.centerIn: parent
            font.pixelSize: 22
            color: "#fff"
            font.bold: true
        }

        MouseArea{
            anchors.fill: parent
            onPressed: {
                parent.scale *= .9
            }
            onReleased:{
                parent.scale /= .9
                root.close()
            }
        }
    }
}
